# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : schedule_plan_schema.py
# Time       ：2023/7/7 22:09
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    定时计划序列化类
    Serialize classes schedule plan
"""
import time

from marshmallow import fields
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema

from apis.schedule_plan.models.schedule_plan_models import CrontabPlan, DatePlan


class IntervalPlanSchema(SQLAlchemyAutoSchema):
    job_id = fields.String()
    start_date = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    end_date = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    weeks = fields.Integer()
    days = fields.Integer()
    hours = fields.Integer()
    minutes = fields.Integer()
    seconds = fields.Integer()


class DatePlanSchema(SQLAlchemyAutoSchema):
    job_id = fields.String()
    run_date = fields.DateTime(format="%Y-%m-%d %H:%M:%S")

    class Meta:
        model = DatePlan


class CrontabPlanSchema(SQLAlchemyAutoSchema):
    start_date = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    end_date = fields.DateTime(format="%Y-%m-%d %H:%M:%S")

    class Meta:
        model = CrontabPlan


def get_next_run_time(next_run_time: int):
    time_local = time.localtime(next_run_time)
    return time.strftime("%Y-%m-%d %H:%M:%S", time_local)


class ApschedulerJobsSchema(SQLAlchemyAutoSchema):
    next_run_time = fields.Function(lambda obj: get_next_run_time(obj.next_run_time))
